package znyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import znyl.common.core.page.TableDataInfo;
import znyl.common.exception.base.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import znyl.common.utils.UserThreadLocal;
import znyl.nursing.mapper.ReservationMapper;
import znyl.nursing.domain.Reservation;
import znyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 预约信息Service业务层处理
 * 
 * @author 睢高浩
 * @date 2025-11-13
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     * 
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询预约信息列表
     * 
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation)
    {
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation)
    {
        return reservationMapper.insertReservation(reservation);
    }

    /**
     * 修改预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation)
    {
        return updateById(reservation) == true ? 1 : 0;
    }

    /**
     * 批量删除预约信息
     * 
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) == true ? 1 : 0;
    }

    /**
     * 删除预约信息信息
     * 
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id)
    {
        return removeById(id) == true ? 1 : 0;
    }

    /**
     * 查询取消预约数量
     *  取消预约次数，当天取消预约次数大于3次，则不让预约
     * @param userId
     * @return
     */
    @Override
    public Integer getCancelledReservationCo(Long userId) {
        LocalDateTime now = LocalDateTime.now().toLocalDate().atStartOfDay();// 今天00:00:00
        //在now+24小时
        LocalDateTime end = now.plusHours(24); // 今天24:00:00
        LambdaQueryWrapper<Reservation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Reservation::getUpdateBy, userId) // 用户id
                .eq(Reservation::getStatus, 2)   // 取消预约
                .between(Reservation::getUpdateTime, now, end);
        return  (int) count(queryWrapper);
    }

    /**
     * @param page1
     * @param status
     * @return
     */
    @Override
    public TableDataInfo Ipage(IPage<Reservation> page1, Integer status) {
        //获取用户
        Long userId = UserThreadLocal.getUserId();
        if(ObjectUtil.isEmpty(userId)){
            throw new BaseException("请先登录");
        }
        LambdaQueryWrapper<Reservation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Reservation::getCreateBy, userId);
        queryWrapper.eq(ObjectUtil.isNotEmpty(status),Reservation::getStatus, status);
        IPage<Reservation> page = page(page1, queryWrapper);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setRows(page.getRecords());
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setMsg("请求成功");
        tableDataInfo.setCode(200);
        return tableDataInfo;
    }

    /**
     * @param id
     * @return
     */
    @Override
    public int cancelReservation(Long id) {
        Reservation byId = getById(id);
        if (byId == null){
            throw new RuntimeException("预约不存在");
        }
        byId.setStatus(2);
        byId.setUpdateBy(UserThreadLocal.getUserId()+"");
        return updateById(byId) == true ? 1 : 0;
    }
}
